package com.aplink.generic.util.validation;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidExpression extends ValidationBase {

	private String valExpression;

	public ValidExpression() {
		this.ErrorMessage = "";
		setValExpression("");
	}

	public ValidExpression(final String valexpression, final String errorMessage) {
		this.valExpression = valexpression;
		this.ErrorMessage = errorMessage;
	}

	@Override
	public boolean checkValidation(final Object attValue) {
		// if there is no expression we will always return true
		if (this.valExpression.compareTo("") == 0) {
			return true;
		}
		final String testData = attValue.toString();

		// use the regex defined in config files
		// String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" +
		// "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
		final Pattern pattern = Pattern.compile(this.valExpression);
		final Matcher matcher = pattern.matcher(testData);

		return matcher.matches();
		// here you can use both of Android pattern
		// return
		// android.util.Patterns.EMAIL_ADDRESS.matcher(testData).matches();

	}

	public String getValExpression() {
		return this.valExpression;
	}

	public void setValExpression(final String valExpression) {
		this.valExpression = valExpression;
	}

}
